package com.base.api.cdr;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.base.api.pms.PmsPublicSqlQuery;
import com.base.utils.TreeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.*;

/**
 * @author wnhuang
 * @Package com.base.api.cdr
 * @date 2020-12-19 12:36
 */
@Component
public class PhysicalInformation extends PmsPublicSqlQuery {

    @Override
    public String splicSql(String sql, Map<String, Object> param){
        StringBuilder param1 = new StringBuilder();
        if(param != null){
            if(param.get("visitNumber") != null && !StringUtils.isBlank((String) (param.get("visitNumber")))){
                param1.append(" and visitnumber = '" + param.get("visitNumber") + "'");
            }
            if(param.get("patientId") != null && !StringUtils.isBlank((String) (param.get("patientId")))){
                param1.append(" and PatientID = '" + param.get("patientId") + "'");
            }


        }
        return MessageFormat.format(sql.replace("'","''"), param1.toString());
    };

    @Override
    public Map<String, Object> beforReturn(Map<String, Object> map) {
        List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("Data");
        Map<String, Object> map1 = new HashMap<>();
        map1.put("Data", TreeUtil.listToTree(JSONArray.parseArray(JSONObject.toJSONString(toTree(list))), "label", "pid", "VitalsignDetail"));
        map1.put("code", "0");
        return map1;
    }

    private List<Map<String, Object>> toTree(List<Map<String, Object>> list) {
        Set<Map<String, Object>> list2 = new HashSet<>();
        for (Map<String, Object> map : list) {
            Map<String, Object> map1 = new HashMap<>();

            String yyyy = (String) map.get("MeasureDay");
            map1.put("label", yyyy);
            map1.put("pid", "0");
            map1.put("MeasureDatetime", yyyy);
            map1.put("visitNumber", map.get("VisitNumber"));
            map1.put("ShitCount", map.get("ShitCount"));
            map1.put("SmailCount", map.get("SmailCount"));
            map1.put("TeTongf", map.get("TeTongf"));
            map1.put("Tetongp", map.get("Tetongp"));
            map1.put("DayNum", map.get("DayNum"));
            map1.put("SBP", map.get("SBP"));
            map1.put("DBP", map.get("DBP"));
            map1.put("Weight", map.get("Weight"));
            map1.put("Height", map.get("Height"));
            map1.put("InWater", map.get("InWater"));
            map1.put("OutWater", map.get("OutWater"));
            map1.put("OPorIP", map.get("opOrIp"));
            map1.put("Id", map1.get("label").hashCode());
            map1.put("DayNum", map1.get("DayNum"));
            map.put("label", UUID.randomUUID());
            map.put("Id", UUID.randomUUID());
            map.put("pid", yyyy);
            list2.add(map);
            list2.add(map1);
        }
        List<Map<String, Object>> list1 = new ArrayList<>(list2);
        return list1;
    }
}
